package com.lvmama.pangolin.webmagic.pipeline;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.entity.tc.TC_Product;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
@Component
public class TongCheng_M_TicketListPipeline implements Pipeline{

	/**   
	* @Title: TongCheng_M_TicketListPipeline.java  
	* @Description: TODO(用一句话描述该文件做什么) 
	* @author ZhouTianQi   
	* @date 2017年1月18日 下午3:12:52 
	* @version V1.0   
	*/
	private Logger logger = LoggerFactory.getLogger(TongCheng_M_TicketListPipeline.class);
	/*@Autowired
	private ProductService productService;*/
	@Override
	public void process(ResultItems resultItems, Task task) {
		logger.info("------------"+DateUtil.formatDate(new Date(), "yyyy:MM:dd hh:mm:ss")+"#####Info:TongCheng_M_TicketList_DataOperationPipeline");
		try {
			JSONArray sceneryinfoArray = (JSONArray)resultItems.get("sceneryinfo");
			String cityId = (String)resultItems.get("cityId");
			if(sceneryinfoArray!=null){
				List<TC_Product> productList = new ArrayList<TC_Product>();
				JSONObject productItem = null;
				for(int index=0;index<sceneryinfoArray.size();index++){
					productItem = sceneryinfoArray.getJSONObject(index);
					TC_Product currentProduct = new TC_Product();
					currentProduct.setCompany_code("TC_M");
					currentProduct.setPlatform_code("WAP");
					currentProduct.setCity_code(cityId);
					currentProduct.setScenery_name(productItem.getString("name"));
					currentProduct.setProduct_id(productItem.getString("id"));
					currentProduct.setDetail_url(getProDetailUrl(productItem.getString("id")));
					currentProduct.setTask_id("");
					currentProduct.setCreate_time(new Date());
					currentProduct.setTask_date(new Date());
					productList.add(currentProduct);
				}
				//productService.insertProductList(productList);
			}
		} catch (Exception e) {
			
		}
	}
	public String getProDetailUrl(String productID){
		String DetailUrlPrefix = "http://m.ly.com/scenery/scenerydetail_";
		String DetailUrlSuffix = "_0_0.html";
		return DetailUrlPrefix+productID+DetailUrlSuffix;
	}

}
